home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’93 / HackTV & Example panel comp. / ExampleVideoPanel.c < prev    next >
Text File  |  1993-08-18  |  17KB  |  681 lines

  1. /*
  2.     File:        ExampleVideoPanel.c
  3.  
  4.     Contains:    Example video panel component routines.
  5.  
  6.     Written by:    Gary Woodcock
  7.  
  8.                 Refer to develop Issue 14, "Video Digitizing Under QuickTime",
  9.                 for details on this code.
  10.                 
  11.     Copyright:    © 1992-1993 by Apple Computer, Inc.
  12.  
  13.     Change History (most recent first):
  14.  
  15. */
  16.  
  17. //-----------------------------------------------------------------------
  18. // includes
  19.  
  20. #include "ExampleVideoPanelPrivate.h"
  21. #include <QuickTimeComponents.h>
  22. #include <Errors.h>
  23. #include <Resources.h>
  24. #include <SysEqu.h>
  25.  
  26. //-----------------------------------------------------------------------
  27.  
  28. #ifdef DEBUG_ME
  29.  
  30. // Use this declaration when we're running linked (for debugging)
  31. pascal ComponentResult
  32. ExampleVideoPanelDispatcher (ComponentParameters *params, Handle storage)
  33.                                              
  34. #else
  35.  
  36. // Use this declaration when we're a standalone component
  37. pascal ComponentResult
  38. main (ComponentParameters *params, Handle storage)
  39.  
  40. #endif DEBUG_ME
  41.  
  42. {
  43.     // This routine is the main dispatcher for the component
  44.     
  45.     ComponentResult        result = noErr;
  46.     ComponentFunction    exampleFunction = nil;
  47.     
  48.     // Did we get a Component Manager request code (< 0)?
  49.     if (params->what < 0)
  50.     {
  51.         switch (params->what)
  52.         {
  53.             case kComponentOpenSelect:            // Open request
  54.             {
  55.                 exampleFunction = _ExampleVideoPanelOpen;
  56.                 break;
  57.             }
  58.             case kComponentCloseSelect:            // Close request
  59.             {
  60.                 exampleFunction = _ExampleVideoPanelClose;
  61.                 break;
  62.             }
  63.             case kComponentCanDoSelect:            // Can Do request
  64.             {
  65.                 result = CallComponentFunction (params, 
  66.                     (ComponentFunction) _ExampleVideoPanelCanDo);
  67.                 break;
  68.             }
  69.             case kComponentVersionSelect:        // Version request
  70.             {
  71.                 result = CallComponentFunction (params,
  72.                     (ComponentFunction) _ExampleVideoPanelVersion);
  73.                 break;
  74.             }
  75.             case kComponentTargetSelect:        // Target request not supported
  76.             case kComponentRegisterSelect:        // Register request not supported
  77.             default:                            // Unknown request
  78.             {
  79.                 result = badComponentSelector;
  80.                 break;
  81.             }
  82.         }
  83.     }
  84.     else    // Was it one of our request codes?
  85.     {
  86.         switch (params->what)
  87.         {
  88.             case kSGCPanelGetDitlSelect:        // SGPanelGetDitl request
  89.             {
  90.                 exampleFunction = _ExampleVideoPanelGetDitl;
  91.                 break;
  92.             }
  93.             case kSGCPanelGetTitleSelect:        // SGPanelGetTitle request
  94.             {
  95.                 exampleFunction = _ExampleVideoPanelGetTitle;
  96.                 break;
  97.             }
  98.             case kSGCPanelCanRunSelect:            // SGPanelCanRun request
  99.             {
  100.                 exampleFunction = _ExampleVideoPanelCanRun;
  101.                 break;
  102.             }
  103.             case kSGCPanelInstallSelect:        // SGPanelInstall request
  104.             {
  105.                 exampleFunction = _ExampleVideoPanelInstall;
  106.                 break;
  107.             }
  108.             case kSGCPanelEventSelect:            // SGPanelEvent request
  109.             {
  110.                 exampleFunction = _ExampleVideoPanelEvent;
  111.                 break;
  112.             }
  113.             case kSGCPanelItemSelect:            // SGPanelItem request
  114.             {
  115.                 exampleFunction = _ExampleVideoPanelItem;
  116.                 break;
  117.             }
  118.             case kSGCPanelRemoveSelect:            // SGPanelRemove request
  119.             {
  120.                 exampleFunction = _ExampleVideoPanelRemove;
  121.                 break;
  122.             }
  123.             case kSGCPanelSetGrabberSelect:        // SGPanelSetGrabber request
  124.             {
  125.                 exampleFunction = _ExampleVideoPanelSetGrabber;
  126.                 break;
  127.             }
  128.             case kSGCPanelSetResFileSelect:        // SGPanelSetResFile request
  129.             {
  130.                 exampleFunction = _ExampleVideoPanelSetResFile;
  131.                 break;
  132.             }
  133.             case kSGCPanelGetSettingsSelect:    // SGPanelGetSettings request
  134.             {
  135.                 exampleFunction = _ExampleVideoPanelGetSettings;
  136.                 break;
  137.             }
  138.             case kSGCPanelSetSettingsSelect:    // SGPanelSetSettings request
  139.             {
  140.                 exampleFunction = _ExampleVideoPanelSetSettings;
  141.                 break;
  142.             }
  143.             default:                            // Unknown request
  144.             {
  145.                 result = badComponentSelector;
  146.                 break;
  147.             }
  148.         }
  149.     }
  150.     if (exampleFunction != nil)
  151.     {
  152.         result = CallComponentFunctionWithStorage (storage, params, exampleFunction);
  153.     }
  154.     return (result);
  155. }
  156.                                              
  157. //-----------------------------------------------------------------------
  158.  
  159. pascal ComponentResult
  160. _ExampleVideoPanelOpen (Handle storage, ComponentInstance self)
  161. {
  162.     #pragma    unused (storage)
  163.     
  164.     PanelGlobalsHdl    globals;
  165.     ComponentResult    result = noErr;
  166.     
  167.     #ifdef THINK_C
  168.         SetComponentInstanceA5 (self, *(long *) CurrentA5);
  169.     #endif THINK_C
  170.     
  171.     // Can we open another instance?
  172.     if (CountComponentInstances ((Component) self) <= kMaxExampleVideoPanelInstances)
  173.     {
  174.         // Did we get our storage?
  175.         globals = (PanelGlobalsHdl) NewHandleClear (sizeof (PanelGlobals));
  176.         if (globals != nil)
  177.         {
  178.             // Keep a reference to self
  179.             (**globals).self = (Component) self;
  180.                         
  181.             // Set storage ref
  182.             SetComponentInstanceStorage (self, (Handle) globals);
  183.         }
  184.         else    // NewHandleClear failed
  185.         {
  186.             result = MemError();
  187.         }
  188.     }
  189.     else    // No more instances can be opened
  190.     {
  191.         result = kGenericError;
  192.     }
  193.     return (result);
  194. }
  195.  
  196. //-----------------------------------------------------------------------
  197.  
  198. pascal ComponentResult
  199. _ExampleVideoPanelClose (Handle storage, ComponentInstance self)
  200. {
  201.     PanelGlobalsHdl    globals = (PanelGlobalsHdl) storage;
  202.     ComponentResult    result = noErr;
  203.     
  204.     // Do we have any clean up to do?
  205.     if (globals != nil)
  206.     {
  207.         // Dispose globals
  208.         DisposHandle ((Handle) globals);
  209.     }
  210.     return (result);
  211. }
  212.  
  213. //-----------------------------------------------------------------------
  214.  
  215. pascal ComponentResult
  216. _ExampleVideoPanelCanDo (short selector)
  217. {
  218.     // Case on the request code
  219.     switch (selector)
  220.     {
  221.         // Component Manager request codes
  222.         case kComponentOpenSelect:
  223.         case kComponentCloseSelect:
  224.         case kComponentCanDoSelect:
  225.         case kComponentVersionSelect:
  226.         
  227.         // Sequence grabber panel component request codes
  228.         case kSGCPanelGetDitlSelect:    
  229.         case kSGCPanelGetTitleSelect:    
  230.         case kSGCPanelCanRunSelect:        
  231.         case kSGCPanelInstallSelect:    
  232.         case kSGCPanelEventSelect:        
  233.         case kSGCPanelItemSelect:        
  234.         case kSGCPanelRemoveSelect:        
  235.         case kSGCPanelSetGrabberSelect:
  236.         case kSGCPanelSetResFileSelect:
  237.         case kSGCPanelGetSettingsSelect:
  238.         case kSGCPanelSetSettingsSelect:
  239.         {
  240.             return (true);
  241.         }
  242.         
  243.         // Unsupported or unknown request codes
  244.         case kComponentRegisterSelect:    // Register request not supported
  245.         case kComponentTargetSelect:    // Target request not supported
  246.         default:                        // Not a request code we recognize
  247.         {
  248.             return (false); 
  249.         }
  250.     }
  251. }
  252.  
  253. //-----------------------------------------------------------------------
  254.  
  255. pascal ComponentResult
  256. _ExampleVideoPanelVersion (void)
  257. {
  258.     // Return the version info
  259.     return (exampleVideoPanelInterfaceRevision);
  260. }
  261.  
  262. //-----------------------------------------------------------------------
  263.  
  264. pascal ComponentResult
  265. _ExampleVideoPanelGetDitl (Handle storage, Handle *ditl)
  266. {
  267.     // This routine gets our ditl and hands it back to the sequence grabber
  268.     
  269.     #pragma unused (storage)
  270.  
  271.     ComponentResult    result = noErr;
  272.     Handle            panelDITL = GetResource ('DITL', kExampleVideoPanelDITLID);
  273.     short            savedResFile = CurResFile();
  274.     
  275.     #ifdef THINK_C
  276.     short    myResFile = CurResFile();
  277.     #else
  278.     short    myResFile = OpenComponentResFile ((**globals).self);
  279.     #endif
  280.  
  281.     if ((myResFile != -1) && ((result = ResError()) == noErr))
  282.     {
  283.         UseResFile (myResFile);
  284.         // Did we get the DITL resource okay?
  285.         result = ResError();    
  286.         if (panelDITL != nil)
  287.         {
  288.             // Detach it
  289.             DetachResource (panelDITL);
  290.         }
  291.         UseResFile (savedResFile);
  292.     }
  293.     *ditl = panelDITL;
  294.     
  295.     return (result);
  296. }
  297.  
  298. //-----------------------------------------------------------------------
  299.  
  300. pascal ComponentResult
  301. _ExampleVideoPanelGetTitle (Handle storage, Str255 title)
  302. {
  303.     #pragma unused (storage)
  304.     #pragma unused (title)
  305.     
  306.     // Nothing to do here right now
  307.     return (paramErr);
  308. }
  309.  
  310. //-----------------------------------------------------------------------
  311.  
  312. pascal ComponentResult
  313. _ExampleVideoPanelCanRun (Handle storage, SGChannel c)
  314. {
  315.     #pragma unused (storage)
  316.     #pragma unused (c)
  317.  
  318.     // Our 'thng' resource has the channelFlagHasDependency flag set, 
  319.     // which means that this routine gets called to find out if
  320.     // it can run in the current environment.  This usually means
  321.     // finding out if the panel is compatible with the currently
  322.     // selected digitizer.  This is handy for adding manufacturer
  323.     // specific panels to the settings dialog.  We don't do anything
  324.     // here except SysBeep just to show we actually get called.
  325.     SysBeep(5);
  326.     
  327.     return (noErr);
  328. }
  329.  
  330. //-----------------------------------------------------------------------
  331.  
  332. pascal ComponentResult
  333. _ExampleVideoPanelInstall (Handle storage, SGChannel c, DialogPtr d,
  334.     short itemOffset)
  335. {
  336.     // Do our setup in this routine
  337.     
  338.     #pragma unused (d)
  339.     #pragma unused (itemOffset)
  340.     
  341.     PanelGlobalsHdl    globals = (PanelGlobalsHdl) storage;
  342.     ComponentResult    result = noErr;
  343.  
  344.     // Get our gray pattern
  345.     *(long *)((**globals).grayPat) = *(long *)((**globals).grayPat + 4) = 0x55AA55AA;
  346.     
  347.     // Get current black level
  348.     result = VDGetBlackLevelValue (SGGetVideoDigitizerComponent (c), &((**globals).savedBlackLevel));
  349.     if (result != noErr)
  350.     {
  351.         // Can't get the black level, so we can't do zero black level
  352.         HiliteControl ((ControlHandle) GetItemHandle (d, itemOffset + kZeroBlackButton), 255);
  353.     }
  354.  
  355.     return (noErr);
  356. }
  357.  
  358. //-----------------------------------------------------------------------
  359.  
  360. pascal ComponentResult
  361. _ExampleVideoPanelEvent (Handle storage, SGChannel c, DialogPtr d,
  362.     short itemOffset, EventRecord *theEvent, short *itemHit, Boolean *handled)
  363. {
  364.     // This routine is quite similar to a normal event filter proc.
  365.     
  366.     PanelGlobalsHdl    globals = (PanelGlobalsHdl) storage;
  367.     ComponentResult    result = noErr;
  368.     GrafPtr            savedPort;
  369.     PenState        savedPen;
  370.     
  371.     // Set up the port stuff
  372.     GetPort (&savedPort);
  373.     SetPort (d);
  374.     GetPenState (&savedPen);
  375.                         
  376.     // Assume we don't handle it
  377.     *handled = false;
  378.     
  379.     // Look for a key hit
  380.     if ((theEvent->what == keyDown) || (theEvent->what == autoKey))
  381.     {
  382.         char    theChar = theEvent->message & charCodeMask;
  383.         char    theKeyCode = ((theEvent->message & keyCodeMask) >> 8);
  384.         Boolean    cmdKeyDown = ((theEvent->modifiers & cmdKey) != 0) ? true : false;
  385.         
  386.         if (cmdKeyDown && (theKeyCode == kDKey))
  387.         {
  388.             unsigned short    maxBlackLevel = 0;
  389.             
  390.             // Fake a "Do it" button hit
  391.             FakeDialogButtonHit (d, itemOffset + kZeroBlackButton);
  392.             result = VDSetBlackLevelValue (SGGetVideoDigitizerComponent (c), &maxBlackLevel);
  393.             *itemHit = itemOffset + kZeroBlackButton;
  394.             *handled = true;
  395.         }
  396.         else if (cmdKeyDown && (theKeyCode == kRKey))
  397.         {
  398.             // Fake a "Reset" button hit
  399.             FakeDialogButtonHit (d, itemOffset + kResetButton);
  400.             result = VDSetBlackLevelValue (SGGetVideoDigitizerComponent (c), &((**globals).savedBlackLevel));
  401.             *itemHit = itemOffset + kResetButton;
  402.             *handled = true;
  403.         }
  404.     }
  405.     else if (theEvent->what == updateEvt)
  406.     {
  407.         Rect    r;
  408.         
  409.         // Draw the separator line whenever we get an update cuz I'm lazy
  410.         GetItemBox (d, itemOffset + kSeparator, &r);
  411.         PenPat ((**globals).grayPat);
  412.         FrameRect (&r);
  413.     }
  414.     
  415.     // Restore stuff
  416.     SetPenState (&savedPen);
  417.     SetPort (savedPort);
  418.     
  419.     return (result);
  420. }
  421.  
  422. //-----------------------------------------------------------------------
  423.  
  424. pascal ComponentResult
  425. _ExampleVideoPanelItem (Handle storage, SGChannel c, DialogPtr d,
  426.     short itemOffset, short itemNum)
  427. {
  428.     #pragma unused (d)
  429.     
  430.     PanelGlobalsHdl    globals = (PanelGlobalsHdl) storage;
  431.     ComponentResult    result = noErr;
  432.     short            theItem = itemNum - itemOffset;    // Remember to account for the item offset
  433.     
  434.     // What item got hit?
  435.     switch (theItem)
  436.     {
  437.         case kZeroBlackButton:
  438.         {
  439.             unsigned short    maxBlackLevel = 0;
  440.             
  441.             result = VDSetBlackLevelValue (SGGetVideoDigitizerComponent (c), &maxBlackLevel);
  442.             break;
  443.         }
  444.         case kResetButton:
  445.         {
  446.             result = VDSetBlackLevelValue (SGGetVideoDigitizerComponent (c), &((**globals).savedBlackLevel));
  447.             break;
  448.         }
  449.         default:
  450.         {
  451.             break;
  452.         }
  453.     }
  454.     return (result);
  455. }
  456.  
  457. //-----------------------------------------------------------------------
  458.  
  459. pascal ComponentResult
  460. _ExampleVideoPanelRemove (Handle storage, SGChannel c, DialogPtr d,
  461.     short itemOffset)
  462. {
  463.     #pragma unused (storage)
  464.     #pragma unused (c)
  465.     #pragma unused (d)
  466.     #pragma unused (itemOffset)
  467.     
  468.     // This is where you do your panel related cleanup.  Note that
  469.     // this is different from the cleanup you do in your close component
  470.     // routine.  Basically, this gets called when your ditl is getting
  471.     // removed from the dialog and a new one is being added (like when
  472.     // someone chooses a different panel from the panel popup menu).
  473.     // Examples of stuff you might do here include getting rid of any
  474.     // custom controls or popup menus, or saving panel settings.
  475.     
  476.     return (noErr);
  477. }
  478.  
  479. //-----------------------------------------------------------------------
  480.  
  481. pascal ComponentResult
  482. _ExampleVideoPanelSetGrabber (Handle storage, SeqGrabComponent sg)
  483. {
  484.     PanelGlobalsHdl    globals = (PanelGlobalsHdl) storage;
  485.     
  486.     // Save our grabber
  487.     (**globals).seqGrabber = sg;
  488.     
  489.     return (noErr);
  490. }
  491.  
  492. //-----------------------------------------------------------------------
  493.  
  494. pascal ComponentResult
  495. _ExampleVideoPanelSetResFile (Handle storage, short resRef)
  496. {
  497.     PanelGlobalsHdl    globals = (PanelGlobalsHdl) storage;
  498.     
  499.     // Since we don't have the channelFlagDontOpenResFile flag
  500.     // set in our 'thng' resource, the sequence grabber will 
  501.     // open our resource file for us.
  502.     
  503.     // Save our resfile ref
  504.     (**globals).resRefNum = resRef;
  505.     
  506.     return (noErr);
  507. }
  508.                                                          
  509. //-----------------------------------------------------------------------
  510.  
  511. pascal ComponentResult
  512. _ExampleVideoPanelGetSettings (Handle storage, SGChannel c, UserData *ud, long flags)
  513. {
  514.     #pragma unused (c)
  515.     #pragma unused (flags)
  516.     
  517.     ComponentResult    result = noErr;
  518.     UserData        userStuff = 0L;
  519.     
  520.     // You HAVE to give something valid back as user data when you
  521.     // return from this routine or your panel will fail to install.  
  522.     // What is being asked for basically amounts to a handle to some 
  523.     // state info specific to your panel.  We don't really have any 
  524.     // state info we care about, so we just hand back an empty user 
  525.     // data just to keep everyone happy.
  526.     
  527.     if ((result = NewUserData (&userStuff)) == noErr)
  528.     {
  529.         *ud = userStuff;
  530.     }
  531.     return (result);
  532. }
  533.                                                         
  534. //-----------------------------------------------------------------------
  535.  
  536. pascal ComponentResult
  537. _ExampleVideoPanelSetSettings (Handle storage, SGChannel c, UserData ud, long flags)
  538. {
  539.     #pragma unused (storage)
  540.     #pragma unused (c)
  541.     #pragma unused (ud)
  542.     #pragma unused (flags)
  543.     
  544.     // Here is where you would decode your user data structure and use
  545.     // the info to set the state of your panel items.  Again, we don't
  546.     // really have anything that has any meaningful state in our panel,
  547.     // so we ignore this.
  548.     
  549.     return (noErr);
  550. }
  551.                                                          
  552. //-----------------------------------------------------------------------
  553.  
  554. pascal ComponentResult
  555. _ExampleVideoPanelValidateInput (Handle storage, Boolean *ok)
  556. {
  557.     #pragma unused (storage)
  558.     
  559.     // This is where you do a sanity check on the user-definable
  560.     // items in your panel.  If there are any bad values, you
  561.     // should alert the user somehow, and return false for *ok.
  562.     // If everything's fine, return true.  We don't really have
  563.     // anything to check on in our example, so we return true
  564.     // all the time.
  565.     
  566.     *ok = true;
  567.     return (noErr);
  568. }
  569.  
  570. //-----------------------------------------------------------------------
  571.  
  572. static OSErr
  573. FakeButtonHit (ControlHandle theButton)
  574. {
  575.     OSErr    result = noErr;
  576.     
  577.     if (theButton != nil)
  578.     {
  579.         long    dummyTicks = 0L;
  580.         
  581.         HiliteControl (theButton, inButton);
  582.         Delay (8, &dummyTicks);
  583.         HiliteControl (theButton, 0);
  584.     }
  585.     else
  586.     {
  587.         result = nilHandleErr;
  588.     }
  589.     return (result);
  590. }
  591.  
  592. //-----------------------------------------------------------------------
  593.  
  594. static OSErr
  595. FakeDialogButtonHit (DialogPtr theDialog, short theButtonItem)
  596. {
  597.     OSErr    result = noErr;
  598.     
  599.     if (theDialog != nil)
  600.     {
  601.         result = FakeButtonHit ((ControlHandle) GetItemHandle (theDialog, theButtonItem));
  602.     }
  603.     else
  604.     {
  605.         result = nilHandleErr;
  606.     }
  607.     return (result);
  608. }
  609.  
  610. //-----------------------------------------------------------------------
  611.  
  612. static void
  613. SetUserItem (DialogPtr theDialog, short theItem, Handle userProc)
  614. {
  615.     Handle    item;
  616.     Rect    box;
  617.     short    itemType;
  618.     
  619.     GetDItem (theDialog, theItem, &itemType, &item, &box);
  620.     SetDItem (theDialog, theItem, itemType, userProc, &box);
  621. }
  622.  
  623. //-----------------------------------------------------------------------
  624.  
  625. static Handle
  626. GetItemHandle (DialogPtr theDialog, short theItem)
  627. {
  628.     Handle    item;
  629.     Rect    box;
  630.     short    itemType;
  631.     
  632.     GetDItem (theDialog, theItem, &itemType, &item, &box);
  633.     return (item);
  634. }
  635.  
  636. //-----------------------------------------------------------------------
  637.  
  638. static void
  639. GetItemBox (DialogPtr theDialog, short theItem, Rect *theRect)
  640. {
  641.     Handle    item;
  642.     short    itemType;
  643.     
  644.     GetDItem (theDialog, theItem, &itemType, &item, theRect);
  645. }
  646.  
  647. //-----------------------------------------------------------------------
  648.  
  649. #ifdef THINK_C
  650. #ifdef DEBUG_ME
  651.  
  652. Component
  653. RegisterExampleVideoPanel (void)
  654. {
  655.     ComponentDescription    foo;
  656.     Handle                     h;
  657.     Component                theCompID;
  658.  
  659.       foo.componentType = SeqGrabPanelType;
  660.       foo.componentSubType = 'vide';
  661.       foo.componentManufacturer = 'xmpl';
  662.       foo.componentFlags = channelFlagDontOpenResFile | channelFlagHasDependency;    // Indicate that we want a can run message
  663.       foo.componentFlagsMask = 0L;
  664.  
  665.     PtrToHand ("\pExample (linked)", &h, 17);
  666.     theCompID = RegisterComponent (&foo, (void *)ExampleVideoPanelDispatcher, 0, h, 0, 0);
  667.     if (h != nil)
  668.     {
  669.         DisposHandle (h);
  670.     }
  671.     return (theCompID);
  672. }
  673.  
  674. #endif DEBUG_ME
  675. #endif THINK_C
  676.  
  677. //-----------------------------------------------------------------------
  678.  
  679.  
  680.  
  681.